'''
Created on Dec 11, 2012

@author: david
'''
from StudentPlayers.TheFightingPenguins.__init__ import get_shortest_path
from copy import *

class WallData(object):
    """
    A class containing data about the starting and ending position of walls, and whether they are vertical or horizontal.
    """
    __slots__=('start', 'end', 'vert')
    def __init__(self, r1, c1, r2, c2):
        self.start = r1, c1
        self.end = r2, c2
        if(r1 == r2):
            self.vert = False
        else:
            self.vert = True
        
    def __str__(self):
        return "Start: "+self.start+" End: "+self.end
    
    def __repr__(self):
        return self.__str__()
    
def checkCollision(r1,c1,r2,c2,playerData):
    """
    This function takes the endpoints of a new wall and the playerData, and checks if the wall about to be placed will hit an existing wall
    if the walls will collide returns true, if the wall doesn't collide returns False.
    """
    #r1,c1 = start other wall, r2,c2 = end other wall
    #Check in bounds and NOT along a side
    bool=False
    if(r1>8 or c1>8 or r2>9 or c2>9 or r1<0 or c1<0 or r2<0 or c2<0):
        bool= True
    if((r1==0 and r2==0)or(c1==0 and c2==0)or(r1==8 and r2==8)or(c1==8 and c2==8)):
        bool= True
    for wall in playerData.walls:
        if(r1==wall.start[0] and c1==wall.start[1] and r2==wall.end[0] and c2==wall.end[1]):
            bool= True
        if(r1==wall.start[1] and c1==wall.start[0] and r2==wall.end[1] and c2==wall.end[0]):
            bool= True
        if(r1==r2 and wall.vert):
            if((r1,c1+1)==(wall.start[0]+1,wall.start[1])):
                bool= True
        if(c1==c2 and not wall.vert):
            if((r1+1,c1)==(wall.start[0],wall.start[1]+1)):
                bool= True
        if(r1==r2 and not wall.vert):
            if((r1,c1+1)==(wall.start[0],wall.start[1]) or (r1,c1+1)==(wall.start[0],wall.start[1]+1) or (r1,c1+1)==(wall.start[0],wall.start[1]+2)):
                bool= True
        if(c1==c2 and wall.vert):
            if((r1+1,c1)==(wall.start[0],wall.start[1]) or (r1+1,c1)==(wall.start[0]+1,wall.start[1]) or (r1+1,c1)==(wall.start[0]+2,wall.start[1])):
                bool= True
    if(not bool):    
        bool= notValidWall(r1,c1,r2,c2,playerData)
    return bool

def notValidWall(r1,c1,r2,c2,playerData):
    """
    notValidWall takes the endpoints of a new wall and the playerData. And checks to see if the new wall will prevent a player from 
    reaching "Goal" if a player's path is blocked returns True, if the paths of all players remain clear returns the result of checkCollision.
    """
    playerData2 = deepcopy(playerData)
    playerData2.add_walls(r1, r2, c1, c2)
    shortestPath=[]
    oppPath=[]
    bool = False
    for i in playerData2.playerLocations:
        if(playerData2.playerId == 1 or playerData2.playerId == 2):
            shortestPath = get_shortest_path(playerData2, playerData2.getCurLoc()[0], playerData2.getCurLoc()[1], playerData2.destination, playerData2.getCurLoc()[1])
            if playerData2.playerId == 1:
                oppPath = get_shortest_path(playerData2, playerData2.playerLocations[1][0], playerData2.playerLocations[1][1], 8, playerData2.playerLocations[1][1])
            else:
                oppPath = get_shortest_path(playerData2, playerData2.playerLocations[0][0], playerData2.playerLocations[0][1], 0, playerData2.playerLocations[0][1])
        elif(playerData2.playerId == 3 or playerData2.playerId == 4):
            shortestPath = get_shortest_path(playerData2, playerData2.getCurLoc()[0], playerData2.getCurLoc()[1], playerData2.getCurLoc()[0], playerData2.destination)
            if playerData2.playerId == 3:
                oppPath = get_shortest_path(playerData2, playerData2.playerLocations[3][0], playerData2.playerLocations[3][1], 8, playerData2.playerLocations[3][1])
            else:
                oppPath = get_shortest_path(playerData2, playerData2.playerLocations[2][0], playerData2.playerLocations[2][1], 0, playerData2.playerLocations[2][1])
    if len(shortestPath)<=1:
        bool= True
    elif len(oppPath)<=1:
        bool= True
    else:
        bool= False
    return bool
    
    
    
    
    






